import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import store from '@/store/index'
import { Message } from 'element-ui'
import home from '@/views/Menus/Home/Home.vue'
import userInfo from '@/views/Menus/User/UserInfo.vue'
import userAvatar from '@/views/Menus/User/UserAvatar.vue'
import userPWD from '@/views/Menus/User/UserPWD.vue'
import ArtCate from '@/views/Menus/article/ArtCate.vue'
import ArtList from '@/views/Menus/article/ArtList.vue'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [{
      path: 'home',
      component: home
    }, {
      path: 'user-info',
      component: userInfo
    }, {
      path: 'user-avatar',
      component: userAvatar
    }, {
      path: 'user-pwd',
      component: userPWD
    }, {
      path: 'art-cate',
      component: ArtCate
    }, {
      path: 'art-list',
      component: ArtList
    }]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/' && !store.state.User.token) {
    next('/login')
    Message.error('没有登陆请登录')
  } else {
    next()
  }
})
export default router
